package com.baor.config;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.Map;

@Component
public class JwtConfig {

    @Value("${config.jwt.secret}")
    private String secret;
    @Value("${config.jwt.header}")
    private String header;

    public String createToken(Map<String, Object> claims) {

//        Date now = new Date();
//        Date expiredate = new Date(now.getTime() + expire * 1000);
        String key = null;

        return Jwts.builder()
                .setClaims(claims)
                .setHeaderParam("typ", "JWT")
                .setIssuedAt(new Date())
                .signWith(SignatureAlgorithm.HS512, secret.getBytes(StandardCharsets.UTF_8))
                .compact();
    }

    public Claims parseToken(String token){
        Claims claims = null;
        try{
            claims = Jwts.parser().setSigningKey(secret.getBytes(StandardCharsets.UTF_8)).build().parseClaimsJws(token).getBody();
            return claims;
        }catch(Exception e){
            return null;
        }
    }

}
